package com.shuijianbaozi.child.testdemo;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
/**
 * 测试viewpager 嵌套 activity :包含三个
 * */
public class Main2Activity extends Activity {

    @InjectView(R.id.rb_content)
    RadioButton rbContent;
    @InjectView(R.id.rb_clocks)
    RadioButton rbClocks;
    @InjectView(R.id.rb_my)
    RadioButton rbMy;
    @InjectView(R.id.rg_main)
    RadioGroup rgMain;
    @InjectView(R.id.viewpager)
    ViewPager viewpager;
    private List<View> mViews;  //数据源
    private ActivityAdapter mAdapter;//适配器

    //解决item中activity点击事件没有反应的问题
    private LocalActivityManager manager;
    private Intent intentItem1,intentItem2,intentItem3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.inject(this);

//        initData_(savedInstanceState);

        initData();
    }

    private void initData_(Bundle savedInstanceState) {
        manager = new LocalActivityManager(this, true);
        manager.dispatchCreate(savedInstanceState);

        mViews =new ArrayList<>();
        mAdapter = new ActivityAdapter(mViews);
        intentItem1 = new Intent(Main2Activity.this, ActivityItem1.class);
        View viewItem1 = manager.startActivity("viewID", intentItem1).getDecorView();
        intentItem2= new Intent(Main2Activity.this, ActivityItem2.class);
        View viewItem2 = manager.startActivity("viewID", intentItem2).getDecorView();
        intentItem3 = new Intent(Main2Activity.this, ActivityItem3.class);
        View viewItem3 = manager.startActivity("viewID", intentItem3).getDecorView();
        mViews.add(viewItem1);
        mViews.add(viewItem2);
        mViews.add(viewItem3);

        viewpager.setAdapter(mAdapter);
    }

    /**
     * 没有点击事件的时候可用
     * */
    @Deprecated
    private void initData() {
        mViews =new ArrayList<>();
        mAdapter = new ActivityAdapter(mViews);
        LayoutInflater inflater = LayoutInflater.from(this);
        View viewItem1 = inflater.inflate(R.layout.activity_item1, null);
        View viewItem2 = inflater.inflate(R.layout.activity_item2, null);
        View viewItem3 = inflater.inflate(R.layout.activity_item3, null);
        mViews.add(viewItem1);
        mViews.add(viewItem2);
        mViews.add(viewItem3);

        viewpager.setAdapter(mAdapter);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //显示radiobutton 选中状态
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.rb_content, R.id.rb_clocks, R.id.rb_my})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_content:
                viewpager.setCurrentItem(0);
                break;
            case R.id.rb_clocks:
                viewpager.setCurrentItem(1);
                break;
            case R.id.rb_my:
                viewpager.setCurrentItem(2);
                break;
        }
    }

    private class ActivityAdapter extends PagerAdapter {
        List<View> mDatas;
        public ActivityAdapter(List<View> mViews) {
            mDatas=mViews;
        }

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View v = mDatas.get(position);
            if (v.getParent() != null) {
                container.removeView(v);
            }
            container.addView(v);
            return v;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mDatas.get(position));
        }
    }
}
